package com.cdks.wuxiu.api.controller;

import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.component.huawei.Moderation;
import com.cdks.wuxiu.api.model.dto.CommentReplyInfoDTO;
import com.cdks.wuxiu.api.model.dto.CommentTopicInfoDTO;
import com.cdks.wuxiu.api.model.param.CommentGetReplyListParam;
import com.cdks.wuxiu.api.model.param.CommentGetTopicListParam;
import com.cdks.wuxiu.api.model.param.CommentPublishTopicParam;
import com.cdks.wuxiu.api.model.param.CommentPublishReplyParam;
import com.cdks.wuxiu.api.model.vo.CommentGetTopicVO;
import com.cdks.wuxiu.api.model.vo.CommentTopicInfoVO;
import com.cdks.wuxiu.api.model.vo.UserInfoVO;
import com.cdks.wuxiu.api.service.CommentService;
import com.cdks.wuxiu.api.util.SmsUtils;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.mysql.CommentReplyInfo;
import com.cdks.wuxiu.entity.mysql.CommentTopicInfo;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;


@Api(tags = "评论")
@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService; 


    private int checkContent(String content, StringBuilder hint)
    {
        int result  = Moderation.checkText(content);
        if (result == 0)
        {
            return 0;
        }

        //发送短信通知
        SmsUtils.sendManageNotice("有评论待审");

        //机审异常
        if (result == -10)
        {
            return -1;
        }

        if (result == -12)
        {
            hint.append("温馨提示：请勿泄露手机、微信、QQ、住址等私密信息，注意保护个人隐私，谨防上当受骗。");
        }
        else if (result == -13)
        {
            hint.append("温馨提示：请勿言语攻击、辱骂他人，否则将面临封号风险。");
        }
        else
        {
            hint.append("温馨提示：请勿发布违法违规信息，否则将面临封号风险。");                   
        }

        return -2;
    }

    @ApiOperation("发布评论")
    @PostMapping("/publishTopic")
    public ApiResult<Object> publishTopic(@RequestBody @Valid CommentPublishTopicParam param) {
        StringBuilder hint = new StringBuilder();
        int result  = checkContent(param.getContent(), hint);

        CommentTopicInfo topic  = new CommentTopicInfo();
        topic.setContent(param.getContent());
        topic.setCreateTime(TimeUtils.getTimeSecond());
        topic.setType(param.getType());
        topic.setDestId(param.getDestId());
        topic.setUserId(param.getUserId());
        topic.setStatus(result == 0 ? Constant.AUDIT_STATUS_PASS : Constant.AUDIT_STATUS_WAIT);
        commentService.saveTopic(topic);

        if (result == -2)
        {
            return ApiResult.failed(-10, hint.toString());
        }

        return ApiResult.success();
    }
    
    @ApiOperation("获取评论列表")
    @PostMapping("/getTopicList")
    public ApiResult<Object> getTopicList(@RequestBody @Valid CommentGetTopicListParam param) {
        List<CommentTopicInfoDTO> topicList  = commentService.getTopicList(param.getUserId(), param.getDestId(), param.getDestType(), param.getType(), param.getPage());
        List<CommentGetTopicVO> voList  = new ArrayList<>();
        for (CommentTopicInfoDTO topic : topicList)
        {
            CommentGetTopicVO voTopic  = new CommentGetTopicVO();
            voTopic.setUserInfo(BeanUtil.copyProperties(topic, UserInfoVO.class));
            voTopic.setTopicInfo(BeanUtil.copyProperties(topic, CommentTopicInfoVO.class));
            voTopic.setReplyList(commentService.getReplyList(param.getUserId(), topic.getId(), param.getType(), 0));

            voList.add(voTopic);
        }

        return ApiResult.success(voList);
    }
    
    @ApiOperation("发布回复")
    @PostMapping("/publishReply")
    public ApiResult<Object> publishReply(@RequestBody @Valid CommentPublishReplyParam param) {
        StringBuilder hint = new StringBuilder();
        int result  = checkContent(param.getContent(), hint);

        CommentReplyInfo reply  = new CommentReplyInfo();
        reply.setContent(param.getContent());
        reply.setCreateTime(TimeUtils.getTimeSecond());
        reply.setTopicId(param.getTopicId());
        reply.setToUserId(param.getToUserId());
        reply.setUserId(param.getUserId());
        reply.setStatus(result == 0 ? Constant.AUDIT_STATUS_PASS : Constant.AUDIT_STATUS_WAIT);
        
        commentService.saveReply(reply);

        if (result == -2)
        {
            return ApiResult.failed(-10, hint.toString());
        }
        
        return ApiResult.success();
    }
    
    @ApiOperation("获取回复列表")
    @PostMapping("/getReplyList")
    public ApiResult<Object> getReplyList(@RequestBody @Valid CommentGetReplyListParam param) {
        List<CommentReplyInfoDTO> replyList  = commentService.getReplyList(param.getUserId(), param.getTopicId(), param.getType(), param.getPage());

        return ApiResult.success(replyList);
    }
}
